/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 数据模型测试 - 验证模型的功能正确性
 */

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import '../lib/models/index.dart';

void main() {
  group('MusicItemModel Tests', () {
    test('should create MusicItemModel from basic constructor', () {
      final music = MusicItemModel(
        id: '123',
        title: 'Test Song',
        artist: 'Test Artist',
        album: 'Test Album',
        audioUrl: 'https://example.com/song.mp3',
      );

      expect(music.id, '123');
      expect(music.title, 'Test Song');
      expect(music.artist, 'Test Artist');
      expect(music.album, 'Test Album');
      expect(music.audioUrl, 'https://example.com/song.mp3');
      expect(music.disabled, false);
      expect(music.sourceType, 'url');
    });

    test('should create MusicItemModel from QQ Music data', () {
      final qqMusicData = {
        'songmid': 'qq123',
        'songname': 'QQ Song',
        'singer': [
          {'name': 'QQ Artist', 'mid': 'artist123'}
        ],
        'albumname': 'QQ Album',
        'albummid': 'album123',
        'interval': 240, // 4 minutes
        'strMediaMid': 'media123',
        'pay': {'payplay': 0}, // free song
      };

      final music = MusicItemModel.fromQQMusic(qqMusicData);

      expect(music.id, 'qq123');
      expect(music.title, 'QQ Song');
      expect(music.artist, 'QQ Artist');
      expect(music.artistId, 'artist123');
      expect(music.album, 'QQ Album');
      expect(music.albumId, 'album123');
      expect(music.albumArt,
          'https://y.gtimg.cn/music/photo_new/T002R300x300M000album123.jpg');
      expect(music.duration, Duration(seconds: 240));
      expect(music.sourceType, 'qq_music');
      expect(music.sourceUrl, 'media123');
      expect(music.disabled, false);
    });

    test('should create MusicItemModel from JSON data', () {
      final jsonData = {
        'id': 'json123',
        'title': 'JSON Song',
        'artist': 'JSON Artist',
        'album': 'JSON Album',
        'audioUrl': 'https://example.com/json-song.mp3',
        'duration': 180000, // 3 minutes in milliseconds
        'disabled': false,
      };

      final music = MusicItemModel.fromJson(jsonData);

      expect(music.id, 'json123');
      expect(music.title, 'JSON Song');
      expect(music.artist, 'JSON Artist');
      expect(music.album, 'JSON Album');
      expect(music.audioUrl, 'https://example.com/json-song.mp3');
      expect(music.duration, Duration(milliseconds: 180000));
      expect(music.disabled, false);
    });

    test('should convert MusicItemModel to JSON', () {
      final music = MusicItemModel(
        id: '123',
        title: 'Test Song',
        artist: 'Test Artist',
        album: 'Test Album',
        audioUrl: 'https://example.com/song.mp3',
        duration: Duration(minutes: 3, seconds: 30),
      );

      final json = music.toJson();

      expect(json['id'], '123');
      expect(json['title'], 'Test Song');
      expect(json['artist'], 'Test Artist');
      expect(json['album'], 'Test Album');
      expect(json['audioUrl'], 'https://example.com/song.mp3');
      expect(json['duration'], 210000); // 3:30 in milliseconds
    });

    test('should handle duration text formatting', () {
      final music1 = MusicItemModel(
        id: '1',
        title: 'Song 1',
        artist: 'Artist',
        album: 'Album',
        audioUrl: 'url',
        duration: Duration(minutes: 3, seconds: 45),
      );

      final music2 = MusicItemModel(
        id: '2',
        title: 'Song 2',
        artist: 'Artist',
        album: 'Album',
        audioUrl: 'url',
      );

      expect(music1.durationText, '03:45');
      expect(music2.durationText, '--:--');
    });

    test('should handle display names correctly', () {
      final music1 = MusicItemModel(
        id: '1',
        title: 'Song',
        artist: 'Artist',
        album: 'Album',
        audioUrl: 'url',
      );

      final music2 = MusicItemModel(
        id: '2',
        title: 'Song',
        artist: '',
        album: '',
        audioUrl: 'url',
      );

      expect(music1.displayArtist, 'Artist');
      expect(music1.displayAlbum, 'Album');
      expect(music2.displayArtist, '未知艺术家');
      expect(music2.displayAlbum, '未知专辑');
    });
  });

  group('PlayMode Tests', () {
    test('should have correct display names', () {
      expect(PlayMode.listLoop.displayName, '列表循环');
      expect(PlayMode.singleLoop.displayName, '单曲循环');
      expect(PlayMode.shuffle.displayName, '随机播放');
    });

    test('should have correct icons', () {
      expect(PlayMode.listLoop.icon, Icons.repeat_rounded);
      expect(PlayMode.singleLoop.icon, Icons.repeat_one_rounded);
      expect(PlayMode.shuffle.icon, Icons.shuffle_rounded);
    });

    test('should cycle through modes correctly', () {
      expect(PlayMode.listLoop.next, PlayMode.singleLoop);
      expect(PlayMode.singleLoop.next, PlayMode.shuffle);
      expect(PlayMode.shuffle.next, PlayMode.listLoop);
    });

    test('should identify mode types correctly', () {
      expect(PlayMode.listLoop.isLoop, true);
      expect(PlayMode.singleLoop.isLoop, true);
      expect(PlayMode.shuffle.isLoop, false);

      expect(PlayMode.shuffle.isShuffle, true);
      expect(PlayMode.listLoop.isShuffle, false);
      expect(PlayMode.singleLoop.isShuffle, false);

      expect(PlayMode.singleLoop.isSingleLoop, true);
      expect(PlayMode.listLoop.isSingleLoop, false);
      expect(PlayMode.shuffle.isSingleLoop, false);

      expect(PlayMode.listLoop.isListLoop, true);
      expect(PlayMode.singleLoop.isListLoop, false);
      expect(PlayMode.shuffle.isListLoop, false);
    });
  });

  group('PageState Tests', () {
    test('should have correct state checks', () {
      expect(PageState.loading.isLoading, true);
      expect(PageState.success.isLoading, false);

      expect(PageState.success.isSuccess, true);
      expect(PageState.loading.isSuccess, false);

      expect(PageState.empty.isEmpty, true);
      expect(PageState.success.isEmpty, false);

      expect(PageState.error.isError, true);
      expect(PageState.success.isError, false);

      expect(PageState.noMore.isNoMore, true);
      expect(PageState.success.isNoMore, false);

      expect(PageState.idle.isIdle, true);
      expect(PageState.loading.isIdle, false);
    });
  });
}
